# 运输订单相应功能
from models.user import User
from config import db, admin_address, contract_name, contract_address, contract_abi, redis_service
from results import *
from utils import common_utils,datetime_utils
from flask import jsonify

def create_trans_order(get_json):
    company_address = get_json['company_address']
    procure_index = get_json['procure_index']
    procure_company = get_json['procure_company']
    trans_company = get_json['trans_company']
    clearance_location = get_json['clearance_location']
    entry_location = get_json['entry_location']
    clearance_time = datetime_utils.str_to_timestamp(get_json['clearance_time'])
    entry_time = datetime_utils.str_to_timestamp(get_json['entry_time'])
    situation = get_json['situation']
    # 调用智能合约中createTransOrder
    # 触发此功能不能使用admin用户，应该使用采购公司的地址
    res = common_utils.common_bc_req(company_address, 
                                     contract_name, 
                                     'createTransOrder', 
                                     [int(procure_index), 
                                      procure_company, 
                                      trans_company,
                                      clearance_location,
                                      entry_location, 
                                      clearance_time, 
                                      entry_time, 
                                      int(situation)], 
                                         contract_address,
                                         contract_abi)
    if res.status_code == 200:
        return success_result
    else:
        res_json = json.loads(res.text)
        return gen_result(500, res_json['message'])
    

def query_procure_order_by_company(get_json):
    """
    通过运输公司address查询运输订单
    """
    company_address = get_json['companyAddress']
     
    # 1. 首先通过采购公司的地址获取公司详情
    res = common_utils.common_bc_req(admin_address, 
                                     contract_name, 
                                     'getTransCompany', 
                                     [company_address], 
                                     contract_address,
                                     contract_abi)
    # 2. 通过公司详情中采购订单的索引号获取所有订单信息
    trans_index_list = json.loads(json.loads(res.text)[4])
    tans_value_list = []
    for index in trans_index_list:
        # 这时index为采购订单索引
        res = common_utils.common_bc_req(admin_address, 
                                     contract_name, 
                                     'getTransOrder', 
                                     [index], 
                                     contract_address,
                                     contract_abi)
        r_list = json.loads(res.text)
        obj = {
            "procureIndex":r_list[0],
            "procureCompany" :r_list[1],
            "transCompany" :r_list[2],
            "clearanceLocation" :r_list[3],
            "entryLocation" :r_list[4],
            "clearanceTime" :datetime_utils.timestamp_to_str(int(r_list[5])),
            "entryTime" :datetime_utils.timestamp_to_str(int(r_list[6])),
            "situation" : r_list[7]
        }
        tans_value_list.append(obj)
    return gen_result(200, tans_value_list)
